<script setup lang="ts">
import MateriaCode from '../materiaCode/index.vue';
import CustomerInfo from '../customerInfo/index.vue';
import { editConfig } from '../../config/edit-config';
import { doUpdateOrder } from '@/api/demand/order/index';
import { handleDelKey } from '@/utils/common';
import type { OrderListRecord } from '@/api/demand/order/model/type';
import type { protoSubmitData } from '../../type';
import type { MaterialChooseVO } from '@/api/modeling/ProcessManagement/model/type';
import type { CustomerInformationListInfo } from '@/api/base/customerInformation/model/type';

defineProps<{
    tableRef?: TableRef;
}>();

/**
 * 校验相关
 */
const { resetRelatedValid } = useValid();

/**
 * @: 处理提交数据
 * @param {*} row
 * @return {*}
 */
const arr = [
    'createTime',
    'creator',
    'dataSource',
    'deviationDays',
    'orderProgress',
    'plannedEndTime',
    'plannedQuantity',
    'schedulingStatus',
    'itemName',
    'itemStock',
    'customer'
];
const handleSubmitData = (row: protoSubmitData) => {
    const submitData = handleDelKey(row, arr);

    return [
        {
            ...submitData
        }
    ];
};

/**
 * @: 编辑相关
 * @param {*} ref
 * @return {*}
 */
const baseEditRef = ref<EditRef>();
const check = ref<undefined | boolean>();
const handleEdit = (row: OrderListRecord, isCheck?: boolean) => {
    check.value = isCheck;
    baseEditRef.value?.open(row, isCheck);

    // 选中物料id与选中客户公司id
    selectMaterialCode.value = row.itemCode;
    selectCustomerCode.value = row.customerCode;
};

/**
 * 选择物料编码
 */
const selectMaterialCode = ref<string>(); // 选择的物料编码
const validMateriaId = ref(); // 校验Id
const materiaVisible = ref(false);
const handleMateria = (row: anyObj, id: number | string) => {
    selectMaterialCode.value = row.itemCode;
    validMateriaId.value = id;
    materiaVisible.value = true;
};

/**
 * 选择物料编码，点击保存
 */
const handleMaterialCodeOk = (materialData: MaterialChooseVO) => {
    resetRelatedValid([validMateriaId.value]);

    // 过滤出当前所选的 id 项
    const currentRow = baseEditRef.value?.formData as OrderListRecord;
    currentRow.itemCode = materialData.materialCode as string;
    currentRow.itemName = materialData.materialName as string;
};

/**
 * 选择客户信息
 */
const selectCustomerCode = ref<string>(); // 选择的客户Id
const validCustomerId = ref(); // 校验Id
const tableCustomerIndex = ref(0); // 校验索引
const customerVisible = ref(false);
const handleCustomerInfo = (row: anyObj, id: number | string, index: number = 0) => {
    selectCustomerCode.value = row.customerCode;
    validCustomerId.value = id;
    tableCustomerIndex.value = index;
    customerVisible.value = true;
};

/**
 * 选择客户编码，点击保存
 */
const handleCustomerCodeOk = (customerData: CustomerInformationListInfo) => {
    resetRelatedValid([validCustomerId.value], tableCustomerIndex.value);

    // 过滤出当前所选的 id 项
    const currentRow = baseEditRef.value?.formData as OrderListRecord;
    currentRow.customerCode = customerData.customerCode;
    currentRow.customerCompanyName = customerData.customerName;
};

defineExpose({
    handleEdit
});
</script>
<template>
    <base-edit
        ref="baseEditRef"
        :edit-config="editConfig"
        :updateFn="doUpdateOrder"
        :table-ref="tableRef"
        :handle-submit-data="handleSubmitData"
    >
        <!-- 物料编码下拉框 -->
        <template #itemCode="{ row, item }">
            <q-virtual-select
                v-model="row.itemCode"
                placeholder="请选择"
                :disabled="check"
                @click="handleMateria(row, item.validProps?.id!)"
                @clear="row.itemName = ''"
            />
        </template>

        <!-- 客户编码下拉框 -->
        <template #customerCode="{ row, item }">
            <q-virtual-select
                v-model="row.customerCode"
                placeholder="请选择"
                :disabled="check"
                @click="handleCustomerInfo(row, item.validProps?.id!)"
                @clear="row.customerCompanyName = ''"
            />
        </template>
    </base-edit>

    <!-- 物料编码 -->
    <MateriaCode
        v-model:visible="materiaVisible"
        :code="selectMaterialCode"
        @ok="handleMaterialCodeOk"
        @close="baseEditRef?.close()"
    />

    <!-- 客户编码 -->
    <CustomerInfo
        v-model:visible="customerVisible"
        :code="selectCustomerCode"
        @ok="handleCustomerCodeOk"
        @close="baseEditRef?.close()"
    />
</template>
<style scoped lang="scss">
@include b(checked-option) {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: calc(100% - 1px);
    padding: 0 20px;
    margin-right: -30px;
    margin-left: -20px;
    border-bottom: 1px dashed #ccc;
}
</style>
